package com.kinglone.util;


import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;

public class StringUtil extends StringUtils
{
  public static String WORD_SEPARATE = "-";

  public static boolean isExistZH(String str)
  {
    String regEx = "[\\u4e00-\\u9fa5]";
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(str);

    return (!(m.find()));
  }

  public static boolean isAllZH(String str)
  {
    String regEx = "^[\\u4e00-\\u9fa5]+$";
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(str);
    return m.matches();
  }

  public static String upperFirstChar(String s)
  {
    if (!(isEmpty(s))) {
      return String.valueOf(s.charAt(0)).toUpperCase() + s.substring(1);
    }
    return s;
  }

  public static String lowerFirstChar(String s)
  {
    if (!(isEmpty(s))) {
      return String.valueOf(s.charAt(0)).toLowerCase() + s.substring(1);
    }
    return s;
  }

  public static String getUpperFirstChar(String s)
  {
    if (!(isEmpty(s))) {
      return String.valueOf(s.charAt(0)).toUpperCase();
    }
    return s;
  }

  public static String toNuSicen(Object value)
  {
    return toNuSicen(value, 2);
  }

  public static String toNuSicen(Object value, int precision)
  {
    Object result = "";
    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setGroupingUsed(false);
    if (value instanceof BigDecimal)
      result = value;
    else if (value instanceof String)
      result = new BigDecimal(String.valueOf(value));
    else if (value instanceof Number)
      result = Double.valueOf(ValueUtil.getDouble(value));
    else {
      throw new RuntimeException(value + "need extends Number or String");
    }
    return df.format(result);
  }

  public static Pattern getInsensitivePattern(String value)
  {
    return Pattern.compile(ValueUtil.getString(value).replaceAll("([\\+\\-\\&\\.\\|\\!\\(\\)\\{\\}\\[\\]\\^\\\"\\~\\*\\?\\:])", "\\\\$1"), 34);
  }

  public static String getRandomAsString(int max, int min)
  {
    return String.valueOf(Math.round(Math.random() * (max - min) + min));
  }

  public static boolean hasEmptyStr(String[] strs)
  {
    for (String str : strs) {
      if (isEmpty(str)) {
        return true;
      }
    }
    return false;
  }

  public static boolean allEmptyStr(String[] strs) {
    for (String str : strs) {
      if (!(isEmpty(str))) {
        return false;
      }
    }
    return true;
  }

  public static String arrayToString(String[] strs)
  {
    if ((strs == null) || (strs.length == 0)) {
      return "";
    }

    StringBuilder sb = new StringBuilder();
    for (String str : strs) {
      sb.append(str).append(",");
    }
    return sb.toString().substring(0, sb.toString().length() - 1);
  }
}